package com.cccyy.xml.xstream;

import com.cccyy.xml.vo.*;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;

/**
 * XStream工具类
 *
 * @author Risk
 * 2018年7月17日
 */
public class XStreamUtil {

    private static XStream xStream;

    static {
        xStream = new XStream(new DomDriver());
        //类加载 必须
        xStream.processAnnotations(Service.class);
        xStream.processAnnotations(Data.class);
        xStream.processAnnotations(Field.class);
        xStream.processAnnotations(SysHeader.class);
        xStream.processAnnotations(AppHeader.class);
        xStream.processAnnotations(LocalHeader.class);
//        XStream.setupDefaultSecurity(xStream);
        // 要关联的类
        xStream.allowTypes(new Class[]{TestXStreamBack.class});
    }

    //xml转java对象
    public static Object xmlToBean(String xml) {
        return xStream.fromXML(xml);
    }

    //java对象转xml
    public static String beanToXml(Object obj) {
        return "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + xStream.toXML(obj);
    }
}
